package cn.lili.modules.payment.entity.dos;

import cn.lili.common.utils.CurrencyUtil;
import cn.lili.modules.payment.entity.dto.OutOrderItem;
import cn.lili.modules.payment.wechat.model.profitsharing.ReceiverResponse;
import cn.lili.mybatis.model.BaseStandardEntity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 分账记录详情-分账接收方
 */
@Data
@TableName("li_out_order_detail_log")
@ApiModel(value = "分账记录详情-分账接收方")
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class OutOrderDetailLog extends BaseStandardEntity {

    /**
     * 商城系统中的订单编号
     */
    @ApiModelProperty(value = "订单单号")
    private String orderSn;
    /**
     * 分账出资的电商平台二级商户，
     * 填写微信支付分配的商户号。
     */
    @ApiModelProperty(value = "二级商户号")
    private String subMchid;

    @ApiModelProperty(value = "二级商户号名称")
    private String subMchName;

    /**
     * 商户系统内部的分账单号，
     * 在商户系统内部唯一（单次分账、多次分账、完结分账应使用不同的商户分账单号），
     * 同一分账单号多次请求等同一次。
     */
    @ApiModelProperty(value = "商户分账单号")
    private String outOrderNo;

    @ApiModelProperty(value = "分账金额")
    private Double amount;

    @ApiModelProperty(value = "分账单描述")
    private String description;

    /**
     * PENDING：待分账
     * SUCCESS：分账成功
     * CLOSED：已关闭
     * 示例值：SUCCESS
     */
    @ApiModelProperty(value = "分账单状态")
    private String result;

//    @ApiModelProperty(value = "完成时间")
//    private DateTime finishTime;

    @ApiModelProperty(value = "分账失败原因")
    private String failReason;

    @ApiModelProperty(value = "分账明细单号")
    private String detailId;

    @ApiModelProperty(value = "重复订单数量")
    @TableField(exist = false)
    private Integer orderCount;

    @ApiModelProperty(value = "重复订单合计金额")
    @TableField(exist = false)
    private Double sumAmount;

    public OutOrderDetailLog(OutOrderLog outOrderLog, ReceiverResponse receiverResponse) {
        this.orderSn = outOrderLog.getOrderSn();
        this.subMchid = receiverResponse.getReceiver_mchid();
        this.subMchName = receiverResponse.getReceiver_account();
        this.outOrderNo = outOrderLog.getOutOrderNo();
        this.amount = CurrencyUtil.reversalFen(receiverResponse.getAmount());
        this.description = receiverResponse.getDescription();
        this.result = receiverResponse.getResult();
        //this.finishTime = DateUtil.parse(receiverResponse.getFinish_time(), "yyyy-MM-dd'T'HH:mm:ss.SSS'+08:00'");
        this.failReason = receiverResponse.getFail_reason();
        this.detailId = receiverResponse.getDetail_id();


    }

    public OutOrderDetailLog(String orderSn, String outOrderNo, OutOrderItem outOrderItem) {
        this.orderSn = orderSn;
        this.outOrderNo = outOrderNo;
        this.amount = outOrderItem.getPrice();
        this.subMchid = outOrderItem.getUserId();
        this.subMchName = outOrderItem.getNickname();
        this.description = outOrderItem.generateDescription();
    }


    /**
     * 生成分账描述
     *
     * @param orderSn 订单号
     * @param amount  补差金额
     * @return
     */
    public static String generateDescriptionText(String orderSn, Double amount) {
        return String.format("订单[%s]分账,金额[%f]", orderSn, amount);
    }

    /**
     * 生成补差给平台描述
     *
     * @param orderSn 订单号
     * @param amount  补差金额
     * @return 补差描述
     */
    public static String generateSubsidiesToPlatform(String orderSn, Double amount) {
        return String.format("订单[%s]，金额[%f]。分账执行，先将已有部分结算给平台，再由平台统一结算。", orderSn, amount);
    }

}
